<?php

namespace App\Http\Controllers\Admin\Hotel;

use App\Http\Controllers\Admin\CommonController;
use App\Models\Area;
use App\Models\Hotel\Hotel;
use App\Models\Hotel\HotelFacility;
use App\Requests\Hotel\StoreRequest;
use Illuminate\Http\Request;
use App\Exceptions\ExceptionSms;
use Carbon\Carbon;
use App\Helpers\MoneyHelper;

/**
 * 酒店管理
 * Class HotelController
 * @package App\Http\Controllers\Admin\Hotel
 */
class HotelController extends CommonController
{
    protected $model = Hotel::class;

    public function index()
    {
        $search_forms = Hotel::getForms(['name']);

        $cols = [[
            ['field' => 'name', 'title' => '名称'],
            ['field' => 'thumbnail_format', 'title' => '封面', 'escape' => false],
            ['field' => 'faddress', 'title' => '地址'],
            ['field' => 'score', 'title' => '评分'],
            ['field' => 'action', 'title' => '操作', 'templet' => '#list-bar'],
        ]];

        $permission_code = 'hotel-query';

        $uri = '/admin/hotel/list?code=' . $permission_code;

        $top_btns = [
            ['code' => 'hotel-add']
        ];

        $row_btns = [
            ['code' => 'hotel-edit'],
            ['code' => 'hotel-delete'],
        ];

        return view('components.list', compact('search_forms', 'cols', 'top_btns', 'row_btns', 'permission_code', 'uri', 'row_btns'));
    }

    public function dataList()
    {
        $list = Hotel::bgBaseQuery($this->input())->paginate($this->limit);
        foreach ($list as $resource) {
            $resource->setAppends(['faddress']);
            $resource->thumbnail_format = $resource->formatImage($resource->thumbnail);
        }
        return ExceptionSms::homeList($list->items(), $list->total());
    }

    public function editAdd()
    {
        $id = $this->input('id') ?? 0;

        $data = $id ? Hotel::getOneById($id) : null;

        $save_forms                          = Hotel::getForms(['name', 'thumbnail', 'area_ids', 'address', 'score', 'contact_tel', 'facility_ids', 'desc'], 'save');
        $save_forms['facility_ids']['width'] = '80%';
        if ($data) {
            $save_forms['facility_ids']['value'] = get_pluck_to_array($data->facilities, 'id');
            $save_forms['thumbnail']['value']    = $data->thumbnail;
            $save_forms['area_ids']['value']     = array_values(array_filter([$data->province_id, $data->city_id, $data->district_id]));
        }
        $permission_code = $id ? 'hotel-edit-save' : 'hotel-add-save';

        return view('components.save_form', compact('data', 'save_forms', 'permission_code'));
    }

    public function dataSave(StoreRequest $request)
    {
        $id = $request->get('id') ?? "";
        if ($id) $data = Hotel::getOneById($id);
        else $data = new Hotel();
        $data->setBgArea($request);
        $data->name        = $request->get("name");
        $data->thumbnail   = $request->get("thumbnail");
        $data->address     = $request->get("address") ?? "";
        $data->score       = $request->get("score") ?? 0;
        $data->contact_tel = $request->get("contact_tel") ?? "";
        $data->desc        = $request->get("desc") ?? "";
        $data->save();

        $old_facility_ids = HotelFacility::whereHotelId($data->id)->pluck('facility_id')->toArray();
        $facility_ids     = $request->facility_ids;
        $facility_ids     = json_decode($facility_ids, true);
        $diff             = collect($facility_ids)->diff($old_facility_ids)->toArray();
        if (count($diff)) {
            $insert_data = [];
            foreach ($facility_ids as $facility_id) {
                $insert_data[] = [
                    'hotel_id'    => $data->id,
                    'facility_id' => $facility_id
                ];
            }
            HotelFacility::whereHotelId($data->id)->delete();
            HotelFacility::insert($insert_data);
        }
        return ExceptionSms::returnSuccess();
    }
}
